'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.rdf.Property import Property
from base.rdf.impl.ResourceImpl import ResourceImpl
from base.shared.JenaException import JenaException
from base.vocabulary.RDF import RDF

# import org.apache.jena.enhanced.*;
# import org.apache.jena.graph.*;
# import org.apache.jena.rdf.model.*;
# import org.apache.jena.shared.*;
# import org.apache.jena.vocabulary.RDF;
# import org.slf4j.Logger;
# import org.slf4j.LoggerFactory;

'''
 * An implementation of Property.
'''


class PropertyImpl(ResourceImpl, Property):
    # @SuppressWarnings("hiding")
    # param 
    # result Implementation factory = new
    # def Implementation factory = Implementation(self):
    #     # @Override
    # # param Node n, EnhGraph eg
    # # result boolean
    # def canWrap(self, n, eg):
    #         return n.isURI()
    #
    #
    #     # @Override
    # # param Node n, EnhGraph eg
    # # result EnhNode
    # def wrap(self, n, eg):
    #         return PropertyImpl(n, eg)

    # param PropertyImpl.class
    # result static Logger logger = LoggerFactory.getLogger(self)

    # protected int
    ordinal = -1

    '''
     * Creates PropertyImpl
    '''

    # param String uri
    def __init__(self):
        super().__init__()

    def initWithUri(self, uri):
        super().initWithUri(uri)
        return self

    # param String namespace, String localName
    def initWithNamespaceAndLocalName(self, namespace, localName):
        super().initWithNamespaceAndLocalName(namespace, localName)
        return self

    # param String uri, ModelCom m
    def initWithUriAndModelCom(self, uri, m):
        super().initWithUriAndModelCom(uri, m)
        return self

    # param String nameSpace, String localName, ModelCom m
    def initWithNameSpaceAndLocalNameAndModelCom(self, nameSpace, localName, m):
        super().initWithNameSpaceAndLocalNameAndModelCom(nameSpace, localName, m)
        return self

    # param Node n, EnhGraph m
    def initWithNodeAndEnhGraph(self, n, m):
        super().initWithNodeAndEnhGraph(n, m)
        return self

    # param Model m
    # result Property
    def inModel(self, m):
        return self.getModel() == self if m else m.createProperty(self.getURI())

    # param String nameSpace, String localName, int ordinal, ModelCom m
    # def __init__(self, nameSpace, localName, ordinal, m):
    #     super().__init__(nameSpace, localName, m)

    # @Override
    # param 
    # result boolean
    def isProperty(self):
        return True

    # @Override
    # param 
    # result int
    def getOrdinal(self):
        if (self.ordinal < 0):
            self.ordinal = self.computeOrdinal()
        return self.ordinal

    # param 
    # result int
    def computeOrdinal(self):
        # String
        localName = self.getLocalName()
        if (self.getNameSpace().equals(RDF.getURI()) and localName.matches("_[0-9]+")):
            return self.parseInt(localName.substring(1))
        return 0

    # param String digits
    # result int
    def parseInt(self, digits):
        try:
            return int(digits)
        except ValueError:
            raise JenaException("checkOrdinal fails on " + digits)
